#!/bin/bash

echo "📊 Jar Monitor 状态检查"
echo ""

# 检查进程
if [ -f "app.pid" ]; then
    PID=$(cat app.pid)
    if ps -p $PID > /dev/null; then
        echo "✅ 应用运行中"
        echo "   PID: $PID"
        echo ""
        
        # 检查端口
        if lsof -i :8080 > /dev/null 2>&1; then
            echo "✅ 端口 8080 已监听"
        else
            echo "⚠️  端口 8080 未监听"
        fi
        
        echo ""
        echo "🌐 访问地址:"
        echo "   Web: http://localhost:8080"
        echo "   Prometheus: http://localhost:8080/actuator/prometheus"
        echo ""
        
        # 健康检查
        echo "🏥 健康检查:"
        HEALTH=$(curl -s http://localhost:8080/actuator/health 2>/dev/null)
        if [ $? -eq 0 ]; then
            echo "$HEALTH" | jq '.' 2>/dev/null || echo "$HEALTH"
        else
            echo "⚠️  无法访问健康检查端点"
        fi
        
        echo ""
        echo "📈 模拟器状态:"
        STATUS=$(curl -s http://localhost:8080/api/simulator/status 2>/dev/null)
        if [ $? -eq 0 ]; then
            echo "$STATUS" | jq '.' 2>/dev/null || echo "$STATUS"
        else
            echo "⚠️  无法获取模拟器状态"
        fi
        
    else
        echo "❌ 应用未运行 (PID 文件存在但进程不存在)"
        rm -f app.pid
    fi
else
    # 尝试查找进程
    PID=$(ps -ef | grep "jar-monitor.jar" | grep -v grep | awk '{print $2}')
    if [ -n "$PID" ]; then
        echo "✅ 应用运行中 (未找到 PID 文件)"
        echo "   PID: $PID"
    else
        echo "❌ 应用未运行"
    fi
fi
